//package com.cictec.middleware.gps.engine.position.handler.alarms;
//
//import java.text.ParseException;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import com.cictec.middleware.commons.utils.Constants;
//import com.cictec.middleware.commons.utils.DateUtils;
//import com.cictec.middleware.commons.utils.PMSUtils;
//import com.cictec.middleware.commons.utils.ZoneUtils;
//import com.cictec.middleware.core.entity.SystemWarnSetting;
//import com.cictec.middleware.core.entity.SystemWarnSettingZone;
//import com.cictec.middleware.core.entity.SystemWarnSettingZoneTime;
//import com.cictec.middleware.core.entity.WarnMsg;
//import com.cictec.middleware.fiegn.distribute.SendPlatformMessageToTerminalService;
//import com.cictec.middleware.gps.engine.BusInstance;
//import com.cictec.middleware.gps.engine.TimingPoint;
//import com.cictec.middleware.gps.engine.exception.BusPositionWarnHandlerException;
//import com.cictec.middleware.gps.engine.position.handler.BusPositionAlarmsHandler;
//import com.cictec.middleware.gps.servic.SystemWarnSettingService;
//import com.cictec.middleware.gps.servic.WarnMsgService;
//
///**
// * 超速报警处理
// * 
// * @author ql
// * @date 2017年9月26日
// */
////@Component
//public class BusOverSpeedingWarnHandler implements WarnHandler {
//
//	private Logger logger = LoggerFactory.getLogger(BusPositionAlarmsHandler.class);
//
//	@Autowired
//	private SystemWarnSettingService systemWarnSettingService;
//	@Autowired
//	private WarnMsgService warnMsgService;
//	@Autowired
//	private SendPlatformMessageToTerminalService sendPlatformMessageToTerminalService;
//	
//	private long durationSeconds = 0;	// 限速报警阀值（秒）
//	private List<SystemWarnSettingZone> overSpeedZones = new ArrayList<SystemWarnSettingZone>();
//	private Map<String, TimingPoint> startTimingPointMap = new HashMap<String, TimingPoint>(); 
//	
//	@Override
//	public String getWarnType() {
//		return Constants.VAL_BUS_STATUS_8;
//	}
//
//	@Override
//	public void stopWarn(BusInstance busInstance) {
//		// 空实现
//	}
//	
//	@Override
//	public void onHandler(BusInstance busInstance) throws BusPositionWarnHandlerException {
//		logger.debug("【超速报警信息处理】");
//		
//		SystemWarnSetting overSpeedSystemWarnSetting = systemWarnSettingService.selectSystemWarnSettingByOverSpeed();
//		if(overSpeedSystemWarnSetting == null){
//			return;
//		}
//		/*
//		 * 1、判定是否开启超速报警设置 2、由区域限速，以区域限速为准，没有则以全局限速为准
//		 */
//		String lineUuid = busInstance.getLineUuid();
//		String lineType = busInstance.getLineType();
//		String busUuid = busInstance.getBusUuid();
//		String lat = busInstance.getLatitude();
//		String lng = busInstance.getLongitude();
//		Date time = busInstance.getTime();
//		String speed = busInstance.getSpeed();
//		
//		if(isOverSpeed(lineUuid, lineType, speed, lat, lng, time)){
//			/*记录第一个超速点 */
//			if(!startTimingPointMap.containsKey(busUuid)){
//				startTimingPointMap.put(busUuid, busInstance);
//				return;
//			}
//			TimingPoint startOverSpeedPoint = startTimingPointMap.get(busUuid);
//			long secondInterval = DateUtils.secondInterval(startOverSpeedPoint.getTime(), busInstance.getTime());
//			
//			if(durationSeconds > 0 && durationSeconds < secondInterval){
//				// 超过时间阀值，开始报警
//				String handle = overSpeedSystemWarnSetting.getSwHanding(); //0：自动、1：手动
//				if(handle.equals(0)){
//					// 发送提示语音
//					sendPlatformMessageToTerminalService.sendTextVoiceMessage(busInstance.getDevCode(), busInstance.getDevLineId(), "司机同志： 您已超速");
//				}else{
//					createWarnMsg(busInstance);
//				}
//			}
//		}else{
//			// 停止报警
//			clearWarn(busInstance);
//		}
//	}
//	
//	public void createWarnMsg(BusInstance busInstance){
//		
//		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_8);
//		if(warnMsg == null){
//			warnMsg = new WarnMsg();
//			warnMsg.setWarnBusUuid(busInstance.getBusUuid());
//			warnMsg.setWarnDevUuid(busInstance.getDevUuid());
//			if (!PMSUtils.isEmpty(busInstance.getDriverUuid())) {
//				warnMsg.setWarnDrvUuid(busInstance.getDriverUuid());
//			}
//			warnMsg.setWarnLineUuid(busInstance.getLineUuid());
//			warnMsg.setWarnSpeed(PMSUtils.isNull(busInstance.getSpeed()));
//			warnMsg.setWarnStartLat(busInstance.getLatitude());
//			warnMsg.setWarnStartLng(busInstance.getLongitude());
//
//			warnMsg.setWarnStartTime(DateUtils.formatDateTimes(busInstance.getTime()));
//			warnMsg.setWarnType(Constants.VAL_BUS_STATUS_8);
//			warnMsg.setWarnStorageTime(DateUtils.formatDateTimes(new Date()));
//			
//			warnMsgService.saveWarn(warnMsg);
//			busInstance.setWarns(Constants.VAL_BUS_STATUS_8, warnMsg);
//			logger.debug("【超速报警】");
//		}
//	}
//	
//	public void clearWarn(BusInstance busInstance){
//		startTimingPointMap.remove(busInstance.getBusUuid());// 移除超度第一个点
//		// 清除超速报警信息
//		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_8);
//		if (warnMsg != null) {
//			warnMsg.setWarnDuration(PMSUtils.isNull(DateUtils.secondInterval(busInstance.getTime(), DateUtils.parseDateTimes(warnMsg.getWarnStartTime()))));
//			warnMsg.setWarnEndTime(DateUtils.formatDateTimes(busInstance.getTime()));
//			warnMsg.setWarnEndLat(busInstance.getLatitude());
//			warnMsg.setWarnEndLng(busInstance.getLongitude());
//			warnMsg.setWarnUpdateTime(DateUtils.formatDateTimes(busInstance.getTime()));
//			warnMsgService.saveWarn(warnMsg);
//		}
//		busInstance.setWarns(Constants.VAL_BUS_STATUS_8, null);
//	}
//	
//	/**
//	 * 判定是否超速
//	 * @return
//	 */
//	public boolean isOverSpeed(String lineUuid, String lineType, String speed, String lat, String lng, Date time) {
//		// 先判断区域，在判定全局
//		if(isOverSpeedByRegion(lineUuid, lineType, speed, lat, lng, time)){
//			return true;
//		}else if(isOverSpeedByAllLine(speed)){
//			return true;
//		}
//		return false;
//	}
//	
//	/**
//	 * 是否超过全局限速设置
//	 * @return
//	 */
//	private boolean isOverSpeedByAllLine(String speed) {
//		boolean flag = false;
//		SystemWarnSetting overSpeedSystemWarnSetting = systemWarnSettingService.selectSystemWarnSettingByOverSpeed();
//		if(overSpeedSystemWarnSetting != null){
//			String maxSpeed = PMSUtils.isNull(overSpeedSystemWarnSetting.getSwParam1());			// 全局最大限速
////		String durationSeconds = overSpeedSystemWarnSetting.getSwParam2();	// 限速报警阀值（分钟）
//			if(PMSUtils.isNotEmpty(maxSpeed)){
//				if(Integer.parseInt(speed) > Integer.parseInt(maxSpeed)){
//					flag = true;
//				}
//			}
//		}
//		return flag;
//	}
//
//	/**
//	 * 是否超过区域限速设置
//	 * @return
//	 */
//	private boolean isOverSpeedByRegion(String lineUuid, String lineType, String speed, String lat, String lng, Date time) {
//		boolean flag = false;
//		// 先判定区域，在判域时间
//		for (SystemWarnSettingZone systemWarnSettingZone : overSpeedZones) {
////			List<ZoneFenceEntity> points = systemWarnSettingZone.getPoints();
////			if (ZoneUtils.isInZoneFence(points, lat, lng)) {	// 自方法需要重新生成围栏，
//			if (ZoneUtils.isInZoneFence(systemWarnSettingZone.getZoneFence(), lat, lng)) {	// 此方法是在获取围栏位置点的时候已经生成了围栏信息
//				List<SystemWarnSettingZoneTime> zoneTimes = systemWarnSettingZone.getZoneTimes();
//				if (isInRegionSpeedLimitTime(zoneTimes, lineUuid, lineType, speed, time)) {
//					// 在围栏内部，并且超速
//					flag = true;
//				}
//			}
//		}
//		return flag;
//	}
//
//	/**
//	 * 【区域限速】是否在区域限速时间范围内
//	 * @return
//	 */
//	private boolean isInRegionSpeedLimitTime(List<SystemWarnSettingZoneTime> zoneTimes, String lineUuid, String lineType, String speed, Date time) {
//		boolean flag = false;
//		try {
//			for (SystemWarnSettingZoneTime systemWarnSettingZoneTime : zoneTimes) {
//				if(systemWarnSettingZoneTime.getWztLineUuid().contains(lineUuid)
//						&& lineType.equals(systemWarnSettingZoneTime.getWztLineType())){
//					Date startTime = DateUtils.parseDate(systemWarnSettingZoneTime.getWztSpeedStartTime(), "HH:mm");// HH:mm
//					Date endTime = DateUtils.parseDate(systemWarnSettingZoneTime.getWztSpeedEndTime(), "HH:mm");// HH:mm
//					if(DateUtils.isInBetweenDate(time, startTime, endTime)
//							&& Integer.parseInt(speed) > systemWarnSettingZoneTime.getWztMaxSpeed()){
//						flag = true;
//					}
//				}
//			}
//		} catch (ParseException e) {
//		}
//		return flag;
//	}
//
//}
